//
//  SPSelectScrollView.m
//  penzer
//
//  Created by 还带大道 on 2018/11/6.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "SPSelectScrollView.h"

@interface SPSelectScrollView()<UIScrollViewDelegate>
@property (nonatomic,strong) UIButton *lastSelectedBtn;

@end

@implementation SPSelectScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self initUI];
    }
    return self;
}


- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super initWithCoder:aDecoder]){
        [self initUI];
    }
    return self;
}


- (void)initUI
{
    self.backgroundColor = [UIColor whiteColor];
    
    // add scrollView
    UIScrollView *scrollView = [UIScrollView new];
    //        scrollView.backgroundColor = [UIColor blueColor];
    self.scrollView = scrollView;
    scrollView.delegate = self;
    scrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:scrollView];
    
    //指示条
    UIView *indicatorBar = [UIView new];
    _indicatorBar = indicatorBar;
    [self.scrollView addSubview:indicatorBar];
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    self.scrollView.frame = self.bounds;
    CGSize size = self.scrollView.frame.size;
    CGFloat margin = self.item_margin;
    __block CGFloat width = 0;
    [self.btns enumerateObjectsUsingBlock:^(UIButton * _Nonnull btn, NSUInteger idx, BOOL * _Nonnull stop) {
        [btn sizeToFit];
        CGFloat x =0;
        if(idx == 0){
            x= self.firstItem_leftMargin;
        }else{
            x= CGRectGetMaxX(self.btns[idx-1].frame)+margin;
        }
        
        if(self.average){
            width = (size.width - self.firstItem_leftMargin - self.btns.count*margin) / self.btns.count;
        }else{
            width = btn.frame.size.width+self.item_inset*2;
        }
        btn.frame = CGRectMake(x, 0, width, size.height);
    }];
    self.scrollView.contentSize = CGSizeMake(CGRectGetMaxX(self.btns.lastObject.frame), 0);
}



- (void)setIndicatorBar_height:(CGFloat)indicatorBar_height
{
    _indicatorBar_height = indicatorBar_height;

}


- (void)setIndicatorBarColor:(UIColor *)indicatorBarColor
{
    _indicatorBarColor = indicatorBarColor;
    _indicatorBar.backgroundColor = self.indicatorBarColor;
}

- (void)setItem_textColor_nomal:(UIColor *)item_textColor_nomal
{
    _item_textColor_nomal = item_textColor_nomal;
    [self.btns enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj setTitleColor:item_textColor_nomal forState:UIControlStateNormal];
    }];
}


- (void)setItem_textColor_selected:(UIColor *)item_textColor_selected
{
    _item_textColor_selected = item_textColor_selected;
    [self.btns enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj setTitleColor:item_textColor_selected forState:UIControlStateSelected];
    }];
}

- (void)setItem_font_nomal:(UIFont *)item_font_nomal
{
    _item_font_nomal = item_font_nomal;
    [self.btns enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.titleLabel.font = item_font_nomal;
    }];
}


- (void)setItems:(NSArray<NSString *> *)items
{
    _items = items;
    [items enumerateObjectsUsingBlock:^(NSString * _Nonnull str, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [UIButton new];
        [btn setTitle:str forState:UIControlStateNormal];
        [btn setTitleColor:self.item_textColor_nomal forState:UIControlStateNormal];
        [btn setTitleColor:self.item_textColor_selected forState:UIControlStateSelected];
        btn.titleLabel.font = self.item_font_nomal;
        btn.tag = idx;
        [btn addTarget:self action:@selector(topBtnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:btn];
        [self.btns addObject:btn];
    }];
    [self layoutIfNeeded];
}


- (void)selectedWithIndex:(NSInteger)index
{
    if(index < self.btns.count){
        [self topBtnOnClick:self.btns[index]];
    }
}


- (void)topBtnOnClick:(UIButton *)btn
{
    _lastSelectedBtn.selected = !_lastSelectedBtn.isSelected;
    _lastSelectedBtn.titleLabel.font = self.item_font_nomal;
    btn.selected = !btn.isSelected;
    btn.titleLabel.font = self.item_font_selected;
    _lastSelectedBtn = btn;
    
    CGFloat x = btn.frame.origin.x + btn.titleLabel.frame.origin.x-_item_inset;
    CGFloat width = btn.titleLabel.width+2*_item_inset;
    _indicatorBar.frame = CGRectMake(x, self.scrollView.frame.size.height-self.indicatorBar_height, width, self.indicatorBar_height);
    
    !self.btnOnClickBlock?:self.btnOnClickBlock(btn.tag);
}



- (NSMutableArray<UIButton *> *)btns
{
    if(!_btns){
        _btns = [NSMutableArray array];
    }
    return _btns;
}

@end
